﻿using EventBus;
using System;
using System.Collections.Generic;
using System.Text;

namespace LindDDDSample.SeedWork
{
    public abstract class Entity
    {
        private List<DomainEvent> _domainEvents;
        public IReadOnlyList<DomainEvent> DomainEvents => _domainEvents?.AsReadOnly();
        protected Entity()
        {
            Id = Guid.NewGuid();
            CreateTime = DateTime.Now;
        }
        public Guid Id { get; private set; }
        public DateTime CreateTime { get; private set; }
        public DateTime LastModifyTime { get; set; }

        public void AddDomainEvent(DomainEvent @event)
        {
            _domainEvents = _domainEvents ?? new List<DomainEvent>();
            _domainEvents.Add(@event);
        }
        public void RemoveDomainEvent(DomainEvent @event)
        {
            _domainEvents?.Remove(@event);
        }

        public void ClearDomainEvents()
        {
            _domainEvents?.Clear();
        }
    }
}
